package com.jack.network.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import com.jack.network.NetWorkType;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: dingfeng
 * @date: 2020-04-12
 * @since: 1.0.0
 **/
public class NetworkUtils {

    /**
     * 是否存在有效的网络连接.
     *
     * @return 存在有效的网络连接返回true，否则返回false
     */
    public static boolean isNetWorkAvailable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return networkConnected(connectivityManager.getActiveNetworkInfo());
    }

    private static boolean networkConnected(NetworkInfo networkInfo) {
        return networkInfo != null && networkInfo.isConnected();
    }

    private static boolean isMobileNetwork(NetworkInfo networkInfo) {
        return networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
    }

    public static boolean isWifiNetwork(NetworkInfo networkInfo) {
        return networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
    }


    public static NetWorkType getNetType(Context context) {
        ConnectivityManager cmgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cmgr == null)
            return NetWorkType.NONE;

        NetworkInfo networkInfo = cmgr.getActiveNetworkInfo();
        if (networkInfo == null)
            return NetWorkType.NONE;

        int type = networkInfo.getType();

        if (type == ConnectivityManager.TYPE_WIFI) {
            return NetWorkType.WIFI;
        } else if (type == ConnectivityManager.TYPE_MOBILE) {
            return NetWorkType.MOBILE;
        }

        return NetWorkType.NONE;
    }

    /**
     * 网络是否连接?
     * */
    public  boolean isNetworkAvailable(Context context) {
        ConnectivityManager cmgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cmgr != null) {
            NetworkInfo[] networkInfos = cmgr.getAllNetworkInfo();
            for (NetworkInfo info : networkInfos) {
                if (info.isConnected()) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * wifi 连接?
     * */
    public static boolean isWifiConnected(Context context) {
        ConnectivityManager cmgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cmgr != null) {
            NetworkInfo networkInfo = cmgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (networkInfo != null) {
                return networkInfo.isConnected();
            }
        }
        return false;
    }

    /**
     * 手机 连接?
     * */
    public static boolean isMobileConnected(Context context) {
        ConnectivityManager cmgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cmgr != null) {
            NetworkInfo networkInfo = cmgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (networkInfo != null) {
                return networkInfo.isConnected();
            }
        }
        return false;
    }

    public NetworkInfo getNetworkInfo (Context context) {
        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return connMgr.getActiveNetworkInfo();
    }

}
